{% extends "base.html" %}

{% block head %}
    <link rel="stylesheet" href="{{ theme_asset(request, 'css/board_common.css?ver=1.0.0') }}">
    <link rel="stylesheet" href="{{ theme_asset(request, 'css/qa.css?ver=1.0.0') }}">
{% endblock head %}

{% block title %}{{ qa_config.qa_title }}{% endblock title %}
{% block subtitle %}{{ qa_config.qa_title }}{% endblock subtitle %}

{% block content %}
{% set query_params = request.query_params %}
    {% if qa_config.qa_content_head %}
        {{ qa_config.qa_content_head|safe }}
    {% endif %}

    <div id="bo_qa_list" class="bo_common bo_list">

        <div class="list_category">
            <h2 class="blind">{{ qa_config.qa_title }} 类别</h2>
            <ul>
                <li><a href="{{ request.url.replace_query_params() }}" {% if not query_params.sca %}class="active"{% endif %}>已满</a></li>
                {% for category in categories %}
                <li><a href="{{ request.url.replace_query_params(sca=category) }}" {% if query_params.sca == category %}class="active"{% endif %}>{{ category }}</a></li>
                {% endfor %}
            </ul>
        </div>

        <div class="list_header">

            <div>
                <span>Total {{ total_count|number_format }}枪</span>
                {{ current_page|number_format }} 页数
            </div>

            <ul>
                {% if request.state.is_super_admin %}
                <li>
                    <a href="{{ url_for('qa_config_form') }}" class="btn_admin btn" title="经理">
                        <i class="fa fa-cog fa-spin fa-fw"></i>
                        <span class="blind">经理</span>
                    </a>
                </li>
                {% endif %}
                <li>
                    <button type="button" class="search_opener btn_b01 btn" title="公告板搜索">
                        <i class="fa fa-search" aria-hidden="true"></i>
                        <span class="blind">公告板搜索</span>
                    </button>

                    <div class="list_search">

                        <form name="fsearch" method="get" class="search_modal">
                            <input type="hidden" name="sca" value="{{ query_params.sca }}">
                            <input type="hidden" name="sop" value="{{ query_params.sop|default("or") }}">

                            <div class="modal_title">搜索</div>

                            <label for="sfl" class="blind">搜索目标</label>
                            <select name="sfl" id="sfl">
                                <option value="qa_subject" {{ get_selected(query_params.sfl, 'qa_subject') }}>标题</option>
                                <option value="qa_content" {{ get_selected(query_params.sfl, 'qa_content') }}>内容</option>
                                <option value="qa_name" {{ get_selected(query_params.sfl, 'qa_name') }}>作家</option>
                                <option value="mb_id" {{ get_selected(query_params.sfl, 'mb_id') }}>会员Id</option>
                            </select>

                            <div class="modal_keyword">
                                <label for="stx" class="blind">搜索词<strong class="blind"> 必不可少的</strong></label>
                                <input type="text" name="stx" value="{{ query_params.get("stx", "") }}" id="stx" required="" class="sch_input" size="25" maxlength="15" placeholder=" 请输入您的搜索词">

                                <button type="submit" value="搜索">
                                    <i class="fa fa-search" aria-hidden="true"></i>
                                    <span class="blind">搜索</span>
                                </button>
                            </div>

                            <button type="button" class="modal_close">
                                <i class="fa fa-xmark" aria-hidden="true"></i>
                                <span class="blind">关闭</span>
                            </button>
                        </form>

                        <div class="search_mask"></div>

                    </div>
                </li>
                <li>
                    <a href="{{ url_for('qa_form_write') }}?{{ query_params }}" class="btn_b01 btn" title="登记入伍人员">
                        <i class="fa fa-pencil" aria-hidden="true"></i>
                        <span class="blind">登记入伍人员</span>
                    </a>
                </li>
            </ul>
        </div>

        <form name="fqalist" id="fqalist" action="{{ url_for('qa_delete_list') }}?{{ query_params }}" onsubmit="return fqalist_submit(this);" method="post">
            <input type="hidden" name="token" value="">

            <table class="list_table">
                <caption class="blind">Q&amp;A 列表</caption>
                <thead>
                    <tr>
                        <th scope="col" class="chk_box">
                            <input type="checkbox" id="chkall" onclick="if (this.checked) all_checked(true); else all_checked(false);">
                            <label for="chkall">
                                <span>&nbsp;</span>
                                <b class="blind">当前页面帖子的完整选择</b>
                            </label>
                        </th>
                        <th scope="col">编号</th>
                        <th scope="col">标题</th>
                        <th scope="col">作家</th>
                        <th scope="col">注册日</th>
                        <th scope="col">状态</th>
                    </tr>
                </thead>
                <tbody>
                {% for qa in qa_list %}
                    <tr class="{{ loop.cycle('', 'even') }}">
                        <td class="table_check chk_box">
                            <input type="checkbox" name="chk_qa_id[]" value="{{ qa.qa_id }}" id="chk_qa_id_{{ loop.index }}">
                            <label for="chk_qa_id_{{ loop.index }}">
                                <span>&nbsp;</span>
                                <b class="blind">{{ qa.subject }}</b>
                            </label>
                        </td>
                        <td class="table_wrid">{{ qa.num }}</td>
                        <td class="table_subject">
                            <span class="bo_cate_link">{{ qa.qa_category }}</span>
                            <a href="{{ url_for('qa_view', qa_id=qa.qa_id) }}?{{ query_params }}" class="bo_tit">
                                {{ qa.subject|search_font(query_params.get("stx", ""))|safe }}
                                {% if qa.icon_file %}<i class="fa fa-download" aria-hidden="true"></i>{% endif %}
                            </a>
                        </td>
                        <td class="table_writer">{{ qa.qa_name }}</td>
                        <td class="table_datetime">{{ qa.qa_datetime|datetime_format('%y-%m-%d') }}</td>
                        <td class="table_status">
                            {% if qa.qa_status %}
                                <span class="txt_done">完成答案</span>
                            {% else %}
                                <span class="txt_rdy">等待答案</span>
                            {% endif %}
                        </td>
                    </tr>
                {% else %}
                    <tr><td colspan="6" class="empty_table">Q&A没有列表.</td></tr>
                {% endfor %}
                </tbody>
            </table>

            {{ paging|safe }}

            <div class="list_footer">

                <div></div>

                <ul>
                    {% if request.state.is_super_admin %}
                    <li>
                        <button type="submit" name="btn_submit" value="删除所选内容" title="删除所选内容" onclick="document.pressed=this.value" class="btn btn_b01 btn_admin">
                            <i class="fa fa-regular fa-trash-can" aria-hidden="true"></i>
                            <span class="blind">删除所选内容</span>
                        </button>
                    </li>
                    {% endif %}
                    <li>
                        <a href="{{ url_for('qa_list') }}" class="btn_b01 btn" title="列表">
                            <i class="fa fa-list" aria-hidden="true"></i>
                            <span class="blind">列表</span>
                        </a>
                    </li>
                    <li>
                        <a href="{{ url_for('qa_form_write') }}" class="btn_b01 btn" title="登记入伍人员">
                            <i class="fa fa-pencil" aria-hidden="true"></i>
                            <span class="blind">登记入伍人员</span>
                        </a>
                    </li>
                </ul>

            </div>

        </form>
    </div>

    <script>
        jQuery(function($){
            $(".search_opener").on("click", function() {
                $(".list_search").toggle();
            })
            $('.search_modal .modal_close, .list_search .search_mask').click(function(){
                $('.list_search').hide();
            });
        });

        function all_checked(sw) {
            var f = document.fqalist;
        
            for (var i=0; i<f.length; i++) {
                if (f.elements[i].name == "chk_qa_id[]")
                    f.elements[i].checked = sw;
            }
        }

        function fqalist_submit(f) {
            var chk_count = 0;
        
            for (var i=0; i<f.length; i++) {
                if (f.elements[i].name == "chk_qa_id[]" && f.elements[i].checked)
                    chk_count++;
            }
        
            if (!chk_count) {
                alert(document.pressed + "选择您想要的一个或多个帖子.");
                return false;
            }
        
            if(document.pressed == "删除所选内容") {
                if (!confirm("是否确实要删除所选帖子?\n\n您无法恢复一次删除的材料"))
                    return false;
            }

            f.token.value = generate_token();

            return true;
        }
    </script>

    {% if qa_config.qa_content_tail %}
        {{ qa_config.qa_content_tail|safe }}
    {% endif %}
{% endblock content %}